<!--
 * @Description: 下载附件
-->
<template>
    <div class="download-att-container">
        <!-- 关联文档 -->
        <template v-if="isDoc">
            <div
                v-if="file.mimeType === 'uc'"
                class="flex-wrapper"
                @click="openZxchatMessage(file.description)"
            >
                <slot></slot>
            </div>
            <a
                v-else
                :href="createDocUrl(file)"
                target="_blank"
                class="flex-wrapper"
            >
                <slot></slot>
            </a>
        </template>
        <!-- 表单中的附件 -->
        <a
            v-else-if="isFormFile"
            @click="downloadAtt"
            target="downloadFileFrame"
            class="flex-wrapper"
        >
            <slot></slot>
        </a>
        <!-- 下载 -->
        <a
            v-else
            :href="downloadUrl"
            download
            class="flex-wrapper"
        >
            <slot></slot>
        </a>
    </div>
</template>
<script>
    import globalData from '@/plugins/data';

    export default {
        name: 'DownloadAtt',
        props: {
            file: {
                type: Object,
            },
        },
        created() {
            this.downloadUrl = `${globalData.ctxPath}/fileUpload.do?method=doDownload4html&fileId=${this.file.fileUrl}&v=${this.file.v}&createDate=${this.file.createdate && this.file.createdate.toString().substring(0, 10)}&filename=${encodeURIComponent(this.file.filename)}`;
        },
        computed: {
            // 是否是关联文档
            isDoc() {
                return (this.file.type.toString() === '2' || this.file.type.toString() === '4') && this.file.description;
            },
            isFormFile() {
                return this.file.type.toString() === '3';
            },
        },
        methods: {
            /**
             * @description: 生成表关联文档用的跳转链接
             * @return {String} 返回链接
             */
            createDocUrl() {
                const { mimeType, description, reference, category } = this.file;

                switch (mimeType) {
                    case 'collaboration':
                        return `${globalData.ctxPath}/collaboration/collaboration.do?method=summary&openFrom=glwd&type=&affairId=${description}&baseObjectId=${reference}&baseApp=${category}${CsrfGuard.getUrlSurffix()}`;
                    case 'edoc':
                        return `${globalData.ctxPath}/edocController.do?method=detailIFrame&isQuote=true&from=Done&openFrom=glwd&affairId=${description}&baseObjectId=${reference}&baseApp=${category}${CsrfGuard.getUrlSurffix()}`;
                    case 'km':
                        return `${globalData.ctxPath}/doc.do?method=docOpenIframeOnlyId&openFrom=glwd&docResId=${description}&baseObjectId=${reference}&baseApp=${category}${CsrfGuard.getUrlSurffix()}`;
                    case 'meeting':
                        return `${globalData.ctxPath}/meeting.do?method=view&isQuote=true&state=10&meetingId=${description}&baseObjectId=${reference}&baseApp=${category}${CsrfGuard.getUrlSurffix()}`;
                    case 'info':
                        return `${globalData.ctxPath}/info/infoDetail.do?method=summary&refAffairId=${description}&baseObjectId=${reference}${CsrfGuard.getUrlSurffix()}`;
                }
            },
            /**
             * @description:以前的代码，贴过来。看描述是通过id获取致信的什么title
             * @param {string} chatMessageId 致信消息ID
             * @return {*}
             */
            openZxchatMessage(chatMessageId) {
                const url = `${globalData.ctxPath}/zx/chatmessage.do?method=showChatMessage&chatMessageId=${chatMessageId}`;

                callBackendMethod('zxChatMessageManager', 'getTitle', chatMessageId, {
                    success: function (returnVal) {
                        const dialog = $.dialog({
                            url,
                            width: 656,
                            height: 514,
                            isDrag: false,
                            title: returnVal,
                            targetWindow: getCtpTop(),
                            buttons: [
                                {
                                    id: 'btncancel',
                                    text: $.i18n('common.button.close.label'),
                                    handler: function () {
                                        dialog.close();
                                    },
                                },
                            ],
                        });
                    },
                    error: function (returnVal) {
                        if (typeof console !== 'undefined' && typeof console.log !== 'undefined') {
                            console.log(returnVal);
                        }
                    },
                });
            },
            /**
             * @description 下载附件，原逻辑在v3x-debug.js
             */
            downloadAtt() {
                const { fileUrl, filename, createdate } = this.file;
                
                downloadAttachment(
                    fileUrl,
                    createdate.toString().substring(0, 10),
                    encodeURIComponent(filename),
                );
            },
        },
    };
</script>
<style lang="scss" scoped>
    .download-att-container {
        display: flex;
        align-items: center;
        overflow: hidden;

        > .flex-wrapper {
            flex: 1;
            display: flex;
            align-items: center;
            overflow: hidden;
        }
    }
</style>
